#include "LogEvent.h"
#include <iostream>

using namespace std;

LogEvent::LogEvent(INET_Addr _addr, Mode m, char* s) : addr(_addr), textString(s)
{
	stream = SOCK_Stream();

	if(SERVER == m)
	{
		acceptor = SOCK_Acceptor();
		acceptor.open(addr);
		handle.setConnection(acceptor.getHandle());
	}
	else if(CLIENT == m)
	{
		connector = SOCK_Connector();
		connector.connect(addr, stream);
		handle.setConnection(stream.get_handle());
	}
}

LogEvent::~LogEvent(void)
{

}

void LogEvent::handle_event(Handler<SOCKET> handle, Event_Type et)
{
	const size_t len = 50;
	char buf[len];

	if(READ_EVENT == et)
	{
		buf[stream.recv(&buf,len,0)]='\0';
		if(strlen(buf) > 0)
			cout << endl << "LogEvent read: " << buf << endl << endl;

	}
	else if(WRITE_EVENT == et)
	{
		stream.send(textString,0);
	}
	else if(TIMEOUT_EVENT == et)
	{
		cout << "Timeout has occured in LogEvent<SOCKET>" << endl;
	}
	else if(ACCEPT_EVENT == et)
	{
		acceptor.accept(stream);
	}
	else if(INIT_EVENT == et)
	{
		stream.send("",0);
	}
	else   //ERROR
		cout << "Error: LogEvent, event_type unknown" << endl;
}


